package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impl.CourseContentServiceImp;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {

    //展示对应的课程的章节与课时信息
    public void findSectionAndLessonByCourseId(HttpServletRequest request, HttpServletResponse response){
        try {
            //1、获取参数
            String course_id = request.getParameter("course_id");

            //2、业务处理
            CourseContentService ccs = new CourseContentServiceImp();
            List<Course_Section> list = ccs.findSectionAndLessonByCourseId(Integer.parseInt(course_id));

            //3、返回结果
            String result = JSON.toJSONString(list);

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据课程id 回显课程信息
    public void findCourseById(HttpServletRequest request,HttpServletResponse response){

        try {
            //1、获取参数
            String course_id = request.getParameter("course_id");

            //2、业务处理
            CourseContentService courseContentService = new CourseContentServiceImp();
            Course course = courseContentService.findCourseByCourseId(Integer.parseInt(course_id));

            //3、返回JSON的数据
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name");

            String result = JSON.toJSONString(course, filter);

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存&修改操作
     */
    public void saveOrUpdateSection(HttpServletRequest request, HttpServletResponse response){
        try {
            //1、获取参数,从域对象中获取
            Map<String,Object> map = (Map) request.getAttribute("map");

            //2、创建course_section
            Course_Section section = new Course_Section();

            //3、使用BeanUtils工具类,将map中的数据封装到section
            BeanUtils.populate(section,map);

            //4、业务处理

            CourseContentService courseContentService = new CourseContentServiceImp();

            //判断是否携带id
            if(section.getId() == 0){
                //新增操作
                String result = courseContentService.saveSection(section);
                //5、响应结果
                response.getWriter().print(result);
            }else {
                //修改操作
                String result = courseContentService.updateSection(section);
                response.getWriter().print(result);
            }



        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //修改章节状态
    public void updateSectionStatus(HttpServletRequest request, HttpServletResponse response){
        try {
            //获取参数
            int id = Integer.parseInt(request.getParameter("id"));  //章节id

            int status = Integer.parseInt(request.getParameter("status"));  //章节状态

            //2、业务处理
            CourseContentService ccs = new CourseContentServiceImp();

            String result = ccs.updateSectionStatus(id, status);

            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存和修改课程信息
     *
     */

    public void saveOrUpdateLesson(HttpServletRequest request,HttpServletResponse response){

        try {
            //1、获取对应的参数 从域中获取
            Map<String,Object> map = (Map) request.getAttribute("map");

            //2、创建course_lesson对象
            Course_Lesson course_lesson = new Course_Lesson();

            //3、使用BeanUtils工具类,将map中的数据封装到lesson上
            BeanUtils.populate(course_lesson,map);

            //4、业务处理
            CourseContentService ccs = new CourseContentServiceImp();

            //5、进行相应的判断
            if(course_lesson.getId() == 0){
                //进行新增操作
                String result = ccs.saveLesson(course_lesson);
                response.getWriter().print(result);
            }else {
                String result = ccs.updateLesson(course_lesson);
                response.getWriter().print(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
